/**
<script type="text/javascript">
var SMCK_admin = new SlaveMasterCheckBoxes('fields_resource_admin_ALL', ['fields_resource_admin_guest','fields_resource_admin_member','fields_resource_admin_staff','fields_resource_admin_admin']);
SMCK_admin.init();
</script>
**/
/**
* A checkbox manager Of Slave Master
*
* $Id: SlaveMasterCheckBoxes.js 45 2007-09-04 10:07:53Z etng2004 $
* @version $Revision: 1.1 $
* @author etng (etng2004#gmail.com)
*/

if (!Function.prototype.apply) {
	Function.prototype.apply = function(obj, args) {
		var parameters = new Array();
		if (!obj) {
			obj = window;
		}
		if (!args) {
			args = new Array();
		}

		for (var i = 0; i < args.length; i++) {
			parameters[i] = 'args[' + i + ']';
		}

		obj.__apply = this;
		var result = eval('obj.__apply(' + parameters.join(', ') + ')');
		obj.__apply = null;
		return result;
	}
}

if (!Function.prototype.bind) {
	Function.prototype.bind = function(object) {
		var __method = this;
		return function() {
			__method.apply(object, arguments);
		}
	}
}


var SlaveMasterCheckBoxes = function(oMasterId, oSlaveIds)
{
	this.Master = {};
	this.Slaves = [];
	this.inited = false;

	this.init = function()
	{
		this.Master = document.getElementById(oMasterId);
		this.Master.onclick = this.checkHandler.bind(this);
		for(var i=0,m=oSlaveIds.length;i<m;i++)
		{
			this.Slaves[i] = document.getElementById(oSlaveIds[i]);
			this.Slaves[i].onclick = this.checkHandler.bind(this);
		}
	};
	this.checkHandler = function(e)
	{
			var elem = e?e.target:window.event.srcElement;
			if(elem == this.Master)
			{
					for(var i=0,m=this.Slaves.length;i<m;i++)
					{
						this.Slaves[i].checked = elem.checked;
					}
			}
			else
			{
					var flag =  true;
					for(var i=0,m=this.Slaves.length;i<m;i++)
					{
						if(!this.Slaves[i].checked)
						{
							flag = false;
							break;
						}
					}
					this.Master.checked = flag;
			}
	};
	if(document.getElementById(oMasterId))
	{
		this.init();
		this.inited = true;
	}
};